package com.business.action.platform;

import javax.servlet.http.HttpServletRequest;

import com.business.bean.admin.InterfaceTestBean;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import net.sf.rose.api.InterfaceClassBean;
import net.sf.rose.api.InterfaceMethodBean;
import net.sf.rose.api.annotation.InterfaceClass;
import net.sf.rose.api.annotation.InterfaceMethod;
import net.sf.rose.api.annotation.RequestParam;
import net.sf.rose.jdbc.DBUtils;
import net.sf.rose.jdbc.PageBean;
import net.sf.rose.jdbc.dao.BeanDAO;
import net.sf.rose.jdbc.dao.MapDAO;
import net.sf.rose.jdbc.query.BeanSQL;
import net.sf.rose.jdbc.query.MapSQL;
import net.sf.rose.jdbc.service.Service;
import net.sf.rose.spring.ApplicationUtils;
import net.sf.rose.web.util.WebUtils;

/**
 * 
 * @author fengjian
 * @version 创建时间：2018年7月15日
 * 基本数据库单表的操作接口类
 */
@Controller
@RequestMapping("/com/api")
@InterfaceClass(title = "接口定义类", description = "", sequence = 1000)
public class APIAction {

    /**
     * 获取所有的API接口定义
     */
	@ResponseBody
	@RequestMapping("/list.do")
	@InterfaceMethod(title = "查询列表", description = "获取所有的API接口定义", sequence = 1)
	public PageBean list() {
		PageBean page = new PageBean();
		page.setData(ApplicationUtils.getInterfaceDefineList());
		return page;
	}

    /**
     * 获取指定的接口方法定义
     */
	@ResponseBody
	@RequestMapping("/getClass.do")
	@InterfaceMethod(title = "获取指定接口类定义", description = "根据接口方法名称获取指定的接口方法定义", sequence = 2)
	@RequestParam("{className,字符串,接口的类名称}")
	public PageBean getClass(Service service, String className) {
		PageBean page = new PageBean();
		InterfaceClassBean cb = null;
		for (InterfaceClassBean b : ApplicationUtils.getInterfaceDefineList()) {
			if (b.getName().equals(className)) {
				cb = b;
				break;
			}
		}
		if (cb == null) {
			page.setMessage("没有找到" + className + "定义的接口类名...");
			page.setSuccess(false);
			return page;
		}
		page.setData(cb);
		return page;
	}

    /**
     * 获取指定的接口方法定义
     */
	@ResponseBody
	@RequestMapping("/getMethod.do")
	@InterfaceMethod(title = "获取指定接口定义", description = "根据接口方法名称获取指定的接口方法定义", sequence = 3)
	public PageBean getMethod(Service service, String className, String methodName) {
		PageBean page = new PageBean();
		InterfaceClassBean cb = null;
		for (InterfaceClassBean b : ApplicationUtils.getInterfaceDefineList()) {
			if (b.getName().equals(className)) {
				cb = b;
				break;
			}
		}
		if (cb == null) {
			page.setMessage("没有找到" + className + "定义的接口类名...");
			page.setSuccess(false);
			return page;
		}
		InterfaceMethodBean mb = null;
		for (InterfaceMethodBean m : cb.getMethods()) {
			if (m.getName().equals(methodName)) {
				mb = m;
				break;
			}
		}
		if (mb == null) {
			page.setMessage("没有找到" + methodName + "定义的接口方法名...");
			page.setSuccess(false);
			return page;
		}
		page.setData(mb);
		return page;
	}

    /**
     * 保存测试用例
     */
	@ResponseBody
	@RequestMapping("/save.do")
	@InterfaceMethod(title = "保存测试用例", description = "保存测试用例", sequence = 4)
	public PageBean save(HttpServletRequest request, Service service) {
		PageBean page = new PageBean();
		MapDAO dao = new MapDAO(service);
		MapSQL query = dao.getQuerySQL();
		query.createSaveSql(WebUtils.getRequestData(request));
		return page;
	}

    /**
     * 删除测试用例
     */
	@ResponseBody
	@RequestMapping("/delete.do")
	@InterfaceMethod(title = "删除测试用例", description = "删除测试用例", sequence = 5)
	public PageBean delete(Service service, String tableName) {
		PageBean page = new PageBean();
		/*MapDAO dao = new MapDAO(service);
		MapSQL query = dao.getQuerySQL();
		query.createDeleteSql(tableName, id);
		if (dao.update() == 0) {
			page.setSuccess(false);
			page.setMessage("数据库表没有一条数据被删除！");
		} else {
			// 如果启用了缓存，删除缓存数据以更新
			if (RedisPoolUtils.inUse()) {
				RedisClient c = new RedisClient();
				c.hdelAll(tableName);
				if (updateIndexs != null) {
					for (String s : updateIndexs.split(",")) {
						c.hdelAll(s);
					}
				}
			}
		}*/
		return page;
	}

	/**
	 * 
	 */
	@ResponseBody
	@RequestMapping("/test/list.do")
	public PageBean listTest(HttpServletRequest request, Service service, String requestMapping) {
		PageBean page = new PageBean();
		page.setData(DBUtils.getBeanListByKey(service, InterfaceTestBean.class, "requestMapping", requestMapping));
		return page;
	}
	
	/**
	 * 
	 */
	@ResponseBody
	@RequestMapping("/test/save.do")
	public PageBean saveTest(HttpServletRequest request, Service service, InterfaceTestBean bean) {
		PageBean res = new PageBean();
		if (DBUtils.update(service, bean) > 0) {
			res.setMessage("保存信息成功！");
		} else {
			res.setSuccess(false);
			res.setMessage("保存信息失败！");
		}
		return res;
	}

	/**
	 * 删除
	 */
	@ResponseBody
	@RequestMapping("/test/delete.do")
	public PageBean deleteTest(HttpServletRequest request, Service service, String id) {
		PageBean page = new PageBean();
		BeanDAO dao = new BeanDAO(service);
		BeanSQL query = dao.getQuerySQL();
		query.createDeleteSql(InterfaceTestBean.class, id);
		if(!dao.execute()){
			page.setSuccess(false);
			page.setMessage("接口测试信息删除失败！");
			return page;
		}
		return page;
	}
}
